Skip to content

perf(stmtcache): replace O(n) invalidation scan with O(1) map lookup#2433

Merged
jackc merged 1 commit into
jackc:masterfrom
analytically:lru_invalidset
Nov 28, 2025
Merged

perf(stmtcache): replace O(n) invalidation scan with O(1) map lookup#2433
jackc merged 1 commit into
jackc:masterfrom
analytically:lru_invalidset

Conversation

@analytically

Copy link
Copy Markdown
Contributor

Replace linear slice scan in LRUCache.Put() with hash map lookup for invalidated statement tracking. Maintains both invalidStmts slice and invalidSet map to optimize both Put() and GetInvalidated() operations.

Performance impact (Apple M1 Pro):

  • Baseline: 1.41x faster (178ns → 126ns)
  • 100 invalidations: 1.56x faster (203ns → 131ns)
  • 500 invalidations: 2.21x faster (287ns → 130ns)
  • Worst case (re-adding invalidated): 3.85x faster (570ns → 148ns)

Original implementation degrades from 178ns to 570ns as invalidations accumulate. Optimized version maintains constant ~130ns performance regardless of invalidation count.

Memory overhead: ~8 bytes per invalidated statement. No API changes.

Replace linear slice scan in LRUCache.Put() with hash map lookup for
invalidated statement tracking. Maintains both invalidStmts slice and
invalidSet map to optimize both Put() and GetInvalidated() operations.

Performance impact (Apple M1 Pro):
- Baseline: 1.41x faster (178ns → 126ns)
- 100 invalidations: 1.56x faster (203ns → 131ns)
- 500 invalidations: 2.21x faster (287ns → 130ns)
- Worst case (re-adding invalidated): 3.85x faster (570ns → 148ns)

Original implementation degrades from 178ns to 570ns as invalidations
accumulate. Optimized version maintains constant ~130ns performance
regardless of invalidation count.

Memory overhead: ~8 bytes per invalidated statement. No API changes.

Signed-off-by: Mathias Bogaert <mathias.bogaert@gmail.com>
@jackc jackc merged commit ea09cc0 into jackc:master Nov 28, 2025
14 checks passed
@jackc

jackc commented Nov 28, 2025

Copy link
Copy Markdown
Owner

The code seems sound, though it does seem to be a code path that should be very rarely traversed.

@analytically analytically deleted the lru_invalidset branch November 28, 2025 22:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants